let nodeCounter = 0;

class TreeNode {
  constructor(
    id,
    level,
    element,
    children,
    menu,
    text,
    icon,
    parentNode,
    expanded
  ) {
    this.id = id;
    this.level = level;
    this.element = element;
    this.children = children;
    this.menu = menu;
    this.text = text;
    this.icon = icon;
    this.parentNode = parentNode;
    this.expanded = expanded;
  }

  createNode = (parentNode, tag, text, icon, expanded, menu) => {
    const { rootNode } = this;
    if (!parentNode) parentNode = rootNode;

    let node = new TreeNode(
      `tree-node-${++nodeCounter}`,
      parentNode.level + 1,
      null,
      [],
      menu,
      text,
      icon,
      parentNode,
      expanded
    );

    //如果是尚未创建完成的树，等创建完毕再统一绘制
    //如果是已经创建完成的树，立刻绘制
    if (rootNode.rendered) rootNode.drawNode(parentNode.element, node);

    parentNode.children.push(node);

    return node;
  };
}

class Treeview4 {
  constructor(parentDiv, menu) {
    parentDiv = document.querySelector(parentDiv);
    parentDiv.classList.add("tree");
    parentDiv.innerHTML = "";
    this.parentDiv = parentDiv;

    this.rootNode = new TreeNode(
      `tree-node-${++nodeCounter}`,
      1,
      null,
      [],
      menu,
      "",

    );
  }
}

export default Treeview4;
